package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.client.service.ContentMappingServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.ContentMapping;
import com.hxkj.domain.po.WebsiteAddr;
import com.platform.common.domain.result.ResultDataStruct;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

import static com.hxkj.client.service.taglib.config.LableConstant.CMS_APP_DOWNLOAD;

/**
 * 软件下载
 * 根据传过来的软件id字符串获取软件的标题和外链地址
 *
 * @author chenfangzhou 2018/09/14
 */
@Component
@Slf4j
public class AppDownloadDirective extends AbstractDirective {
    @Autowired
    private ArticleServiceClient articleServiceClient;
    @Autowired
    private ContentMappingServiceClient contentMappingServiceClient;

    //站点id
    private static final String SITE_ID = "siteId";
    //文章id
    private static final String ARTICLE_IDS = "articleIds";
    //排序字段
    private static final String SORT = "sort";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            String articleIds = getString(ARTICLE_IDS, params);
            String sort = StringUtils.isBlank(getString(SORT, params)) ? "update_time" : getString(SORT, params);
            String note = super.getNote(env);

            List<Article> appList;

            if (StringUtils.isBlank(articleIds)) {
                appList = new ArrayList<>();
            } else {
                Article article_ = new Article();
                article_.setArticleIds(articleIds);
                article_.setSiteId(siteId);
                article_.setSort(sort);
                appList = articleServiceClient.getArticleByIds(article_);
                WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);
                if (StringUtils.isNotBlank(note)) {
                    for (Article arc : appList) {
                        arc.setManuscriptPath(webSiteAddr.getIpAdd() + arc.getManuscriptPath());
                    }
                }
                // 添加文章的扩展属性
                for (Article article : appList) {
                    // 判断该文章所在分类是否有扩展属性
                    ContentMapping contentMapping = new ContentMapping();
                    contentMapping.setColumnId(article.getColumnId());
                    ResultDataStruct<List<ContentMapping>> resultDataStruct1 = contentMappingServiceClient.getContentMappingList(contentMapping);
                    if (resultDataStruct1.getData() != null && resultDataStruct1.getData().size() > 0) {
                        // 该分类有扩展属性
                        List<ContentMapping> contentMappingList = resultDataStruct1.getData();
                        setContentMap(article, contentMappingList);
                    }
                }
                Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                Map<Long, String> siteCtxMap =  (Map<Long, String>) enviroment.get("pcSiteCtxMap");
                for (Article item : appList) {
                    // 静态文件文章链接加前缀
                    String urlContext = siteCtxMap.get(item.getSiteId());
                    if(StringUtils.isNotBlank(urlContext)) {
                        item.setManuscriptPath(urlContext + item.getManuscriptPath());
                    }
                }
            }
            env.setVariable("appList", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(appList));

            String userToken = super.getUserToken(env);
            if (body != null) {
                body.render(env.getOut());
                /* if (StringUtils.isEmpty(note)) {

                } else {
                   Map<String, String> maps = new LinkedHashMap<>();

                    maps.put(CMS_APP_DOWNLOAD, "");
                    maps.put(SITE_ID, String.valueOf(getLong(SITE_ID, params)));
                    maps.put(ARTICLE_IDS, getString(ARTICLE_IDS, params));
                    maps.put(SORT, String.valueOf(getLong(SORT, params)));
                    try {
                        super.print(body, maps, env, userToken, params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }*/
            } else {
                log.debug("文章详情所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
